package com.coconet.gas.rest;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.coconet.gas.biz.LeakRepairBiz;
import com.coconet.gas.biz.ReportBiz;
import com.coconet.gas.constant.dwEnum;
import com.coconet.gas.vo.Assets;
import com.coconet.gas.vo.dw.LeakRepair;
import com.coconet.gas.vo.dw.MeiTuoTaskip;
import com.coconet.gas.vo.dw.Report;
import com.coconet.gas.vo.dw.TaskReportRelate;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import response.BaseResponse;
import response.ObjectRestResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/show" )
public class ShowRest {
    @Autowired
    private LeakRepairBiz leakRepairBiz;
    @Autowired
    private ReportBiz reportBiz;

    @ApiOperation(value = "漏洞修复", httpMethod = "GET")
    @GetMapping(value = "/repair/{asset}")
    public BaseResponse repair(@PathVariable String asset) throws IOException {
        LeakRepair leakRepair = new LeakRepair();
        leakRepair.setAsset(asset);
        leakRepair.setTime(new Date());
        leakRepairBiz.insertSelective(leakRepair);
        return new BaseResponse(dwEnum.SUCCEESS.code(), "漏洞修复成功");
    }

    @ApiOperation(value = "展示漏洞修复列表", httpMethod = "GET")
    @GetMapping(value = "/getRepair")
    public ObjectRestResponse getRepair() {
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(), "",leakRepairBiz.selectListAll());
    }

    @ApiOperation(value = "查看报告", httpMethod = "GET")
    @GetMapping(value = "/getReport/{asset}")
    public ObjectRestResponse getReport(@PathVariable String asset) throws IOException {
        LeakRepair leakRepair = new LeakRepair();
        leakRepair.setAsset(asset);
        List<LeakRepair> leakRepairList = leakRepairBiz.selectList(leakRepair);
        Report report = new Report();
        if (leakRepairList.size() > 0) {
            report.setReportId("1");
        } else {
            report.setReportId("2");
        }
        List<Report> reportList = reportBiz.selectList(report);
        return new ObjectRestResponse(dwEnum.SUCCEESS.code(),"",reportList);
    }

    @ApiOperation(value = "扫描报告EXCEL导出", httpMethod = "POST")
    @PostMapping(value = "/exportExcel")
    public void exportEXCEL(HttpServletResponse res, @RequestBody Report report) {
        LeakRepair leakRepair = new LeakRepair();
        leakRepair.setAsset(report.getIp());
        List<LeakRepair> leakRepairList = leakRepairBiz.selectList(leakRepair);
        if (leakRepairList.size() > 0) {
            report.setReportId("1");
        } else {
            report.setReportId("2");
        }
        List<Report> reportList = reportBiz.selectList(report);
        Collections.sort(reportList, (p1, p2) -> Double.valueOf(p2.getSeverity()).compareTo(Double.valueOf(p1.getSeverity())));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("扫描报告", "报告"), Report.class, reportList);
        res.setHeader("Content-Disposition", "attachment;filename=" + "report.xls");
        res.setContentType("application/octet-stream");
        res.setCharacterEncoding("UTF-8");
        OutputStream os = null;
        try {
            os = res.getOutputStream();
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "扫描报告docx导出", httpMethod = "POST")
    @PostMapping(value = "/exportDocx")
    public BaseResponse exportDocx(HttpServletResponse res, @RequestBody Report report) throws IOException, XDocReportException {
        res.setHeader("Content-Disposition", "attachment;filename=" + "report.docx");
        res.setContentType("application/msword");
        res.setCharacterEncoding("UTF-8");
        //获取Word模板，模板存放路径在项目的resources目录下
        InputStream ins = this.getClass().getResourceAsStream("/bug.docx");
        //注册xdocreport实例并加载FreeMarker模板引擎
        IXDocReport docx = XDocReportRegistry.getRegistry().loadReport(ins,
                TemplateEngineKind.Freemarker);
        //创建xdocreport上下文对象
        IContext context = docx.createContext();

        //创建要替换的文本变量
        context.put("ip", report.getIp());

        //创建字段元数据
        FieldsMetadata fm = docx.createFieldsMetadata();
        //Word模板中的表格数据对应的集合类型
        fm.load("reports", Report.class, true);

        //输出到本地目录
        //FileOutputStream out = new FileOutputStream(new File("D://商品销售报表.docx"));

        //导出docx
        docx.process(context, res.getOutputStream());
        return new BaseResponse(dwEnum.SUCCEESS.code(),"导出报告成功");
    }
}
